﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Data;
using System.Collections;

namespace EmProductCenter.pxjd
{
    /// <summary>
    /// CourseInfo 的摘要说明
    /// </summary>
    public class CourseInfo : IHttpHandler
    {
        Common.CommonClass common = new Common.CommonClass();
        BLL.ePioneer_CourseInfo bllEPCI = new BLL.ePioneer_CourseInfo();
        BLL.ePioneer_UserInfo bllEPUI = new BLL.ePioneer_UserInfo();
        BLL.ePioneer_Lecturer bllEPL = new BLL.ePioneer_Lecturer();
        BLL.ePioneer_Reservation bllEPR = new BLL.ePioneer_Reservation();
        public void ProcessRequest(HttpContext context)
        {
            string strAction = context.Request["action"].ToString();
            StringBuilder strReturn = new StringBuilder();
            switch (strAction)
            {
                //获取课程信息，并分页
                case "GetCourseInfo":
                    strReturn = GetCourseInfo(context);
                    break;
                //根据课程ID获取课程信息
                case "GetCourseInfoByTId":
                    strReturn = GetCourseInfoByTId(context);
                    break;
                //忘记密码
                case "ForgetPassWord":
                    strReturn = ForgetPassWord(context);
                    break;
                //修改密码
                case "UpdatePW":
                    strReturn.Append(UpdatePW(context));
                    break;
                //注册，返回值0：注册成功！1：用户名已经存在；2：注册失败
                case "Register":
                    strReturn.Append(Register(context));
                    break;
                //增加下载数
                case "AddCount":
                    strReturn.Append(AddCount(context));
                    break;
                //按月查询课程
                case "GetThisMonthCourse":
                    strReturn = GetThisMonthCourse(context);
                    break;
                //按日查询课程
                case "GetThisDayCourse":
                    strReturn = GetThisDayCourse(context);
                    break;
                //查询课程
                case "SearchCourse":
                    strReturn = SearchCourse(context);
                    break;
                //课程详细信息
                case "GetCourseHtml":
                    strReturn = GetCourseHtml(context);
                    break;
                //获取讲师信息
                case "GetTeachHtml":
                    strReturn = GetTeachHtml(context);
                    break;
                //预约课程，返回-1信息不存在，-2已经预定，0预定成功
                case "Reservation":
                    strReturn.Append(Reservation(context));
                    break;
            }
            context.Response.Write(strReturn);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// 预约课程
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private string Reservation(HttpContext context)
        {
            string strReturn = "-1";
            //线下0，线上1
            string isLine = "0";
            string mobile = "";
            string reMyName = "";
            string id = "";
            string loginName = "";
            if (context.Request["mobile"] != null)
                mobile = context.Request["mobile"];

            if (context.Request["reMyName"] != null)
                reMyName = context.Request["reMyName"];

            if (context.Request["isLine"] != null)
                isLine = context.Request["isLine"];

            if (context.Request["id"] != null)
                id = context.Request["id"];
            if (context.Request["loginName"] != null)
                loginName = context.Request["loginName"];
            string code = "";
            Random rand = new Random();
            code = rand.Next(1000, 9999).ToString();
            int r = bllEPR.Add(id, loginName, code);
            if (r == 0)
            {
                if (isLine == "0")
                {
                    try
                    {
                        //更新用户姓名和手机号
                        bllEPUI.UpdateUserInfo(loginName, reMyName, mobile);
                        //线下会场需要发短信验证码
                        EmoneyService.emoneybusiness client = new EmoneyService.emoneybusiness();
                        client.SendMsg(mobile, "课程预约成功，您的会场预约验证码为：" + code);
                        strReturn = "0";
                    }
                    catch
                    {
                        strReturn = "-1";
                    }
                }
                else
                    strReturn = "0";
            }
            else
            {
                if (isLine == "0" && r == -2)
                {
                    try
                    {
                        //更新用户姓名和手机号
                        bllEPUI.UpdateUserInfo(loginName, reMyName, mobile);
                        DAL.ePioneer_UserInfo bll = new DAL.ePioneer_UserInfo();
                        Model.ePioneer_UserInfo uModel = bll.Login(loginName);
                        List<Model.ePioneer_Reservation> list = bllEPR.GetModelList("UserId='" + uModel.Id + "' and CourseInfoId='" + id + "'");
                        if (list.Count > 0)
                        {
                            code = list[0].Code;
                        }
                        //线下会场需要发短信验证码
                        EmoneyService.emoneybusiness client = new EmoneyService.emoneybusiness();
                        client.SendMsg(mobile, "课程预约成功，您的会场预约验证码为：" + code);
                        strReturn = "0";
                    }
                    catch
                    {
                        strReturn = "-1";
                    }
                }
                else
                {
                    strReturn = r.ToString();
                }
            }
            return strReturn;
        }
        /// <summary>
        /// 获取讲师信息
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private StringBuilder GetTeachHtml(HttpContext context)
        {
            StringBuilder strReturn = new StringBuilder();
            string lecturer = "", location = "";
            DateTime startDate = Convert.ToDateTime(DateTime.Now.Year + "-" + DateTime.Now.Month + "-1");

            if (context.Request["lecturer"] != null)
                lecturer = context.Request["lecturer"];
            if (context.Request["startDate"] != null && context.Request["startDate"] != "")
            {
                DateTime sDate = Convert.ToDateTime(context.Request["startDate"]);
                startDate = Convert.ToDateTime(sDate.Year + "-" + sDate.Month + "-1");
            }
            DateTime endDate = startDate.AddMonths(1);
            //获取某讲师的在某月的所有课程信息
            DataRow[] list = bllEPCI.GetCourseInfo(lecturer, location, startDate, endDate);
            int count = list.Count();
            StringBuilder strCour = new StringBuilder();
            StringBuilder strTeach = new StringBuilder();
            strCour.Append("<p class='panel_footer_title blue'>" + startDate.Month + "月全国课程预告</p>");
            strCour.Append("<table width=\"100%\" border=\"0\" cellspacing=\"6\" cellpadding=\"0\">");
            if (count == 0)
            {
                strCour.Append("<tr><td height=\"24\" class=\"txt_left\">暂无数据</td></tr>");
                Model.ePioneer_Lecturer model = bllEPL.GetModel(lecturer);
                if (model.PicUrl != null && model.PicUrl != "")
                    strTeach.Append("<img src='" + model.PicUrl + "' width=\"74\" height=\"100\" />");
                else
                    strTeach.Append("<img src=\"../image/master_face_1.jpg\" width=\"74\" height=\"100\" />");
                strTeach.Append("<div class=\"master_info\"><p class=\"blue blod\">" + model.Lecturer + "</p><p>益盟操盘手金牌讲师</p><p>专长领域：" + model.Speciality + "</p></div><p class=\"note\">" + model.BriefInfo + "</p>");
            }
            else
            {
                strTeach.Append("<img src=\"../image/master_face_1.jpg\" width=\"74\" height=\"100\" /><div class=\"master_info\"><p class=\"blue blod\">" + list[0]["Lecturer"] + "</p><p>益盟操盘手金牌讲师</p><p>专长领域：" + list[0]["Speciality"] + "</p></div><p class=\"note\">" + list[0]["lBriefInfo"] + "</p>");
            }
            for (int i = 0; i < 8; i++)
            {
                if (i >= count)
                {
                    break;
                }
                DateTime courseStartDate = Convert.ToDateTime(list[i]["CourseStartDate"]);
                strCour.Append("<tr><td height=\"24\" class=\"txt_left\">" + list[i]["Title"] + "</td><td width=\"60\" height=\"24\">" + courseStartDate.ToString("yyyy.MM.dd") + "</td><td width=\"40\" height=\"24\">" + list[i]["Location"] + "</td><td width=\"60\" height=\"24\">");
                if (courseStartDate < DateTime.Now)
                {
                    strCour.Append("<span class='light_grey'>已经结束</apan>");
                }
                else
                {
                    int icount = 0;
                    if (list[i]["IReservation"] != null && list[i]["IReservation"].ToString() != "")
                        icount = Convert.ToInt32(list[i]["IReservation"]) - Convert.ToInt32(list[i]["CountRes"]);
                    if (icount > 0)
                        strCour.Append("<a class=\"blue\" href=\"javascript:void(0);\" onclick=\"Reservation(" + list[i]["Id"] + ")\">我要预约</a>");
                    else
                        strCour.Append("<span class='light_grey'>名额已满</apan>");
                }

                strCour.Append("</td><td width=\"40\" height=\"24\"><a href=\"javascript:void(0);\" onclick=\"OpenCourseInfo(" + list[i]["Id"] + ");\">详情</a></td></tr>");
            }
            strCour.Append("</table>");

            string[] ss = new string[] { strCour.ToString(), strTeach.ToString() };
            strReturn.AppendLine(common.ObjToJson(ss, Encoding.UTF8));
            return strReturn;
        }

        /// <summary>
        /// 课程详细信息
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private StringBuilder GetCourseHtml(HttpContext context)
        {
            StringBuilder strReturn = new StringBuilder();
            int id = 0;
            if (context.Request["id"] != null)
                id = Convert.ToInt32(context.Request["id"]);
            DataRow[] list = bllEPCI.GetCourseInfo(id);
            StringBuilder strHtml = new StringBuilder();
            if (list.Count() > 0)
            {
                DateTime courseStartDate = Convert.ToDateTime(list[0]["CourseStartDate"]);
                strHtml.Append("<div class=\"panel_title\"><p class=\"txt_left\">&nbsp;课程简介</p><a href=\"javascript:void(0);\" class=\"panel_close\" onclick=\"CloseContentDiv();\">close</a></div><div class=\"panel_content\"><div class=\"master_face\">");
                strHtml.Append("<img src=\"../image/master_face_1.jpg\" width=\"74\" height=\"100\" />");
                strHtml.Append(list[0]["Lecturer"] + "</div><p class=\"score_title\">" + list[0]["Title"] + "</p><div class=\"left\"><p>" + list[0]["BriefInfo"].ToString().Replace("\r\n", "<br/>") + "</p><div class=\"iorder\"><p>");
                if (courseStartDate < DateTime.Now)
                {
                    //strHtml.Append("<span class='light_grey'>已经结束</apan>");
                    strHtml.Append("<a href=\"javascript:void(0);\"><img src=\"../image/xxhc_order_full.gif\" width=\"90\" height=\"26\" /></a>");
                }
                else
                {
                    int icount = 0;
                    if (list[0]["IReservation"] != null && list[0]["IReservation"].ToString() != "")
                        icount = Convert.ToInt32(list[0]["IReservation"]) - Convert.ToInt32(list[0]["CountRes"]);
                    if (icount > 0)
                        strHtml.Append("<a href=\"javascript:void(0);\" onclick=\"Reservation('" + list[0]["Id"] + "');\"><img src=\"../image/xxhc_order.jpg\" width=\"90\" height=\"26\" /></a>");
                    else
                        strHtml.Append("<a href=\"javascript:void(0);\"><img src=\"../image/xxhc_order_full.jpg\" width=\"90\" height=\"26\" /></a>");
                }
                strHtml.Append("或拨打：400670 6888 预约</p></div></div><div class=\"score_detail\"><p><span class=\"blue blod\">主讲讲师：</span>" + list[0]["Lecturer"] + " <a class=\"blue txt_uline\" href=\"javascript:void(0);\" onclick=\"OpenTeachInfo('" + list[0]["Lecturer"] + "')\">讲师详情</a></p><p><span class=\"blue blod\">时间：</span>" + courseStartDate.ToString("yyyy-MM-dd hh:mm") + "</p><p><span class=\"blue blod\">地点：</span>" + list[0]["Url"] + "</p><p><span class=\"blue blod\">主讲内容：</span></p><p>" + list[0]["MainContent"].ToString().Replace("\r\n", "<br/>") + "</p></div></div>");
            }
            string[] ss = new string[] { strHtml.ToString() };
            strReturn.AppendLine(common.ObjToJson(ss, Encoding.UTF8));
            return strReturn;
        }

        /// <summary>
        /// 查询课程
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private StringBuilder SearchCourse(HttpContext context)
        {
            StringBuilder strReturn = new StringBuilder();
            int iCount = 10;
            int iPage = Convert.ToInt32(context.Request["page"]);
            string lecturer = "", location = "";
            DateTime startDate = Convert.ToDateTime(DateTime.Now.Year + "-" + DateTime.Now.Month + "-1");

            if (context.Request["lecturer"] != null && context.Request["lecturer"] != "请选择专家")
                lecturer = context.Request["lecturer"];
            if (context.Request["location"] != null && context.Request["location"] != "请选择地区")
                location = context.Request["location"];
            if (context.Request["startDate"] != null && context.Request["startDate"] != "" && context.Request["startDate"] != "月份")
                startDate = Convert.ToDateTime(DateTime.Now.Year + "年" + context.Request["startDate"] + "1日");
            DateTime endDate = startDate.AddMonths(1);
            DataRow[] list = bllEPCI.GetCourseInfo(lecturer, location, startDate, endDate);
            StringBuilder html = new StringBuilder();
            StringBuilder pageStr = new StringBuilder();
            int counts = list.Count();
            //总页数
            int page = counts % iCount == 0 ? counts / iCount : counts / iCount + 1;
            int putinPage = iPage;
            if (iPage >= page)
                iPage = page;
            if (iPage < 1)
                iPage = 1;

            if (counts == 0)
                html.Append("<tr><td>暂无数据</td></tr>");

            for (int i = (iPage - 1) * iCount; i < iPage * iCount; i++)
            {
                if (i >= counts)
                    break;
                DateTime courseStartDate = Convert.ToDateTime(list[i]["CourseStartDate"]);
                html.Append("<tr>");
                html.Append("<td class='txt_left'><a href='javascript:void(0);' onclick=\"OpenCourseInfo('" + list[i]["Id"] + "')\">" + list[i]["Title"] + "</a></td>");
                html.Append("<td width='140' class='txt_left'>" + list[i]["Lecturer"] + "</td>");
                html.Append("<td width='130'>" + courseStartDate.ToShortDateString() + "<br />" + list[i]["StartTime"] + "-" + list[i]["EndTime"] + "</td>");
                html.Append("<td width='120'>" + list[i]["Location"] + "</td>");
                if (courseStartDate < DateTime.Now)
                {
                    html.Append("<td width='110'>已经结束</td>");
                }
                else
                {
                    if (Convert.ToInt32(list[i]["CountRes"]) < Convert.ToInt32(list[i]["IReservation"]))
                        html.Append("<td width='110'><a href='javascript:void(0);' title='请先预约' onclick=\"Reservation('" + list[i]["Id"] + "');\">我要预订</a></td>");
                    else
                        html.Append("<td width='100'>名额已满</td>");
                }
                html.Append("</tr>");
            }

            //分页
            if (iPage == 1)
                pageStr.Append("<a href=\"javascript:void(0)\" class=\"btn txt\"><span>首页</span></a>");
            else
                pageStr.Append("<a href=\"javascript:void(0)\" onclick=\"GetCourseInfo('1')\" class=\"btn txt\"><span>首页</span></a>");
            //绑定上一页
            if (putinPage > 1)
                pageStr.Append("<a href=\"javascript:void(0)\" onclick=\"GetCourseInfo('p')\" class=\"btn txt\" style='width:40px;'><span>上一页</span></a>");
            else
                pageStr.Append("<a href=\"javascript:void(0)\" class=\"btn txt\" style='width:40px;'><span>上一页</span></a>");

            if (iPage > 10)
                pageStr.Append("<a href=\"javascript:void(0)\" onclick=\"GetCourseInfo('b')\" class=\"btn num some\"><span>...</span></a>");
            int j = 1;
            if (iPage > 10)
            {
                iPage = (iPage / 10) * 10 + 1;
                j = iPage;
                for (; j < iPage + 10; j++)
                {
                    if (j > page)
                        break;
                    if (j == iPage)
                        pageStr.Append("<a href=\"javascript:void(0)\" onclick=\"GetCourseInfo('" + j + "')\" class='btn num target'><span>" + j + "</span></a>");
                    else
                        pageStr.Append("<a href='javascript:void(0)' onclick=\"GetCourseInfo('" + j + "')\" class='btn num'><span>" + j + "</span></a>");
                }
            }
            else
            {
                for (; j < 11; j++)
                {
                    if (j > page)
                        break;
                    if (j == iPage)
                        pageStr.Append("<a href=\"javascript:void(0)\" onclick=\"GetCourseInfo('" + j + "')\" class='btn num target'><span>" + j + "</span></a>");
                    else
                        pageStr.Append("<a href='javascript:void(0)' onclick=\"GetCourseInfo('" + j + "')\" class='btn num'><span>" + j + "</span></a>");
                }
            }

            if (j < page)
                pageStr.Append("<a href='javascript:void(0)' onclick=\"GetCourseInfo('g')\" class=\"btn num some\"><span>...</span></a>");
            //绑定下一页
            if (putinPage < page)
                pageStr.Append("<a href='javascript:void(0)' onclick=\"GetCourseInfo('n')\" class=\"btn txt\" style='width:40px;'><span>下一页</span></a>");
            else
                pageStr.Append("<a href='javascript:void(0)' class=\"btn txt\" style='width:40px;'><span>下一页</span></a>");

            if (iPage == page)
                pageStr.Append("<a href='javascript:void(0)' class='btn txt'><span>末页</span></a>");
            else
                pageStr.Append("<a href='javascript:void(0)' onclick=\"GetCourseInfo('" + page + "')\" class='btn txt'><span>末页</span></a>");
            string[] ss = new string[] { html.ToString(), pageStr.ToString() };
            strReturn.AppendLine(common.ObjToJson(ss, Encoding.UTF8));
            return strReturn;

        }

        /// <summary>
        /// 获取当天的课程信息
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private StringBuilder GetThisDayCourse(HttpContext context)
        {
            StringBuilder strReturn = new StringBuilder();
            string lecturer = "", location = "";
            DateTime startDate = DateTime.Now.Date;

            if (context.Request["lecturer"] != null && context.Request["lecturer"] != "请选择专家")
                lecturer = context.Request["lecturer"];
            if (context.Request["location"] != null && context.Request["location"] != "请选择地区")
                location = context.Request["location"];
            if (context.Request["startDate"] != null && context.Request["startDate"] != "")
                startDate = Convert.ToDateTime(context.Request["startDate"]).Date;
            DateTime endDate = startDate.AddDays(1);
            DataRow[] list = bllEPCI.GetCourseInfo(lecturer, location, startDate, endDate);
            int count = list.Count();
            StringBuilder strCour = new StringBuilder();
            strCour.Append("<tr><th scope='col'>课程</th><th scope='col' style='width: 110px;'>专家</th><th scope='col' style='width: 100px;'>时间</th><th scope='col' style='width: 100px;'>地点</th><th scope='col' style='width: 88px;'>预约</th></tr>");
            if (count == 0)
                strCour.Append("<tr><td>暂无数据</td><td></td><td></td><td></td><td></td></tr>");
            for (int i = 0; i < count; i++)
            {
                DateTime courseStartDate = Convert.ToDateTime(list[i]["CourseStartDate"]);
                strCour.Append("<tr>");
                strCour.Append("<td><a href='javascript:void(0);' onclick=\"OpenCourseInfo('" + list[i]["Id"] + "')\">" + list[i]["Title"] + "</a></td>");
                strCour.Append("<td width='110'>" + list[i]["Lecturer"] + "</td>");
                strCour.Append("<td width='100'>" + list[i]["StartTime"] + "-" + list[i]["EndTime"] + "</td>");
                strCour.Append("<td width='100'>" + list[i]["Location"] + "</td>");
                if (courseStartDate < DateTime.Now)
                {
                    strCour.Append("<td width='88'>已经结束</td>");
                }
                else
                {
                    if (Convert.ToInt32(list[i]["CountRes"]) < Convert.ToInt32(list[i]["IReservation"]))
                        strCour.Append("<td width='88'><a href='javascript:void(0);' title='请先预约' onclick=\"Reservation('" + list[i]["Id"] + "');\">我要预订</a></td>");
                    else
                        strCour.Append("<td width='88'>名额已满</td>");
                }
                strCour.Append("</tr>");
            }
            string[] ss = new string[] { strCour.ToString() };
            strReturn.AppendLine(common.ObjToJson(ss, Encoding.UTF8));
            return strReturn;
        }

        /// <summary>
        /// 获取某月的课程信息
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private StringBuilder GetThisMonthCourse(HttpContext context)
        {
            StringBuilder strReturn = new StringBuilder();
            string lecturer = "", location = "";
            DateTime startDate = Convert.ToDateTime(DateTime.Now.Year + "-" + DateTime.Now.Month + "-1");

            if (context.Request["lecturer"] != null && context.Request["lecturer"] != "请选择专家")
                lecturer = context.Request["lecturer"];
            if (context.Request["location"] != null && context.Request["location"] != "请选择地区")
                location = context.Request["location"];
            if (context.Request["startDate"] != null && context.Request["startDate"] != "")
            {
                DateTime sDate = Convert.ToDateTime(context.Request["startDate"]);
                startDate = Convert.ToDateTime(sDate.Year + "-" + sDate.Month + "-1");
            }
            if (context.Request["preOrnext"] != null && context.Request["preOrnext"] != "")
            {
                string preOrnext = context.Request["preOrnext"];
                if (preOrnext == "1")
                    startDate = startDate.AddMonths(-1);
                else if (preOrnext == "2")
                    startDate = startDate.AddMonths(1);
            }

            DateTime endDate = startDate.AddMonths(1);
            DataRow[] list = bllEPCI.GetCourseInfo(lecturer, location, startDate, endDate);
            ArrayList dayList = new ArrayList();
            for (int k = 0; k < list.Count(); k++)
            {
                dayList.Add(Convert.ToDateTime(list[k]["CourseStartDate"]).Day);
            }
            StringBuilder strTr = new StringBuilder();
            int days = endDate.AddDays(-1).Day;
            int i = 1;
            int j = GetThisDay(startDate.DayOfWeek);
            int taday = DateTime.Now.Day;
            strTr.Append("<tr class='title'><th style='height:51px' scope='col'>星期日</th><th scope='col'>星期一</th><th scope='col'>星期二</th><th scope='col'>星期三</th><th scope='col'>星期四</th><th scope='col'>星期五</th><th scope='col'>星期六</th></tr>");
            //控制日期
            while (i <= days)
            {
                strTr.Append("<tr>");
                //控制星期
                for (int k = 0; k < j; k++)
                {
                    strTr.Append("<td>&nbsp;</td>");
                }
                for (; j < 7; j++)
                {
                    if (i <= days)
                    {
                        if (i == taday && startDate == Convert.ToDateTime(DateTime.Now.Year + "-" + DateTime.Now.Month + "-1"))
                        {
                            strTr.Append("<td class='bg3' onclick='GetThisDayCourse(" + i + ");'>" + i + "</td>");
                        }
                        else
                        {
                            if (dayList.Contains(i))
                                strTr.Append("<td class='bg2' onclick='GetThisDayCourse(" + i + ");'>" + i + "</td>");
                            else
                                strTr.Append("<td>" + i + "</td>");
                        }
                    }
                    i++;
                }
                j = 0;
                strTr.Append("</tr>");
            }

            string[] ss = new string[] { strTr.ToString(), startDate.ToString("yyyy年MM月") };
            strReturn.AppendLine(common.ObjToJson(ss, Encoding.UTF8));
            return strReturn;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        private int GetThisDay(DayOfWeek week)
        {
            int result = 0;
            switch (week)
            {
                case DayOfWeek.Tuesday:
                    result = 2;
                    break;
                case DayOfWeek.Wednesday:
                    result = 3;
                    break;
                case DayOfWeek.Thursday:
                    result = 4;
                    break;
                case DayOfWeek.Friday:
                    result = 5;
                    break;
                case DayOfWeek.Saturday:
                    result = 6;
                    break;
                case DayOfWeek.Monday:
                    result = 1;
                    break;
                default:
                    break;
            }
            return result;
        }

        /// <summary>
        /// 增加下载数
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private string AddCount(HttpContext context)
        {
            int id = 0;
            if (context.Request["id"] != null)
                id = Convert.ToInt32(context.Request["id"]);

            return bllEPCI.AddCount(id);
        }

        /// <summary>
        /// 注册，返回值0：注册成功！1：用户名已经存在；2：注册失败
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private string Register(HttpContext context)
        {
            Model.ePioneer_UserInfo model = new Model.ePioneer_UserInfo();
            if (context.Request["loginName"] != null)
            {
                model.LoginName = context.Request["loginName"];
                model.Email = context.Request["loginName"];
            }
            else
            {
                model.LoginName = "";
                model.Email = "";
            }
            if (context.Request["passWord"] != null)
                model.PassWord = context.Request["passWord"];
            else
                model.PassWord = "";
            if (context.Request["mobile"] != null)
                model.Mobile = context.Request["mobile"];
            else
                model.Mobile = "";
            model.UserId = "";
            model.UserName = "";
            return bllEPUI.Register(model);
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private string UpdatePW(HttpContext context)
        {
            string loginName = "", passWord = "";
            if (context.Request["loginName"] != null && context.Request["loginName"] != "")
                loginName = context.Request["loginName"];
            if (context.Request["passWord"] != null && context.Request["passWord"] != "")
                passWord = context.Request["passWord"];
            return bllEPUI.Update(loginName, passWord);
        }

        /// <summary>
        /// 忘记密码
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private StringBuilder ForgetPassWord(HttpContext context)
        {
            StringBuilder strReturn = new StringBuilder();
            string loginName = "";
            if (context.Request["loginName"] != null && context.Request["loginName"] != "")
                loginName = context.Request["loginName"];
            strReturn.Append(bllEPUI.ForgetPassWord(loginName, MsgResource.emailHtml));
            return strReturn;
        }

        /// <summary>
        /// 根据讲师姓名获取讲师的所有课程
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private StringBuilder GetCourseInfoByTId(HttpContext context)
        {
            StringBuilder strReturn = new StringBuilder();
            int iCount = 10, isLine = 1;
            int iPage = Convert.ToInt32(context.Request["page"]);
            string keyWord = "", courseType = "", lecturer = "";
            DateTime startDate = DateTime.Now.AddYears(-10), endDate = DateTime.Now.AddYears(10);
            if (context.Request["keyWord"] != null && context.Request["keyWord"] != "关键字查找")
                keyWord = context.Request["keyWord"];
            if (context.Request["courseType"] != null && context.Request["courseType"] != "课程类别" && context.Request["courseType"] != "全部课程")
                courseType = context.Request["courseType"];
            if (context.Request["lecturer"] != null && context.Request["lecturer"] != "讲师搜索" && context.Request["lecturer"] != "讲师" && context.Request["lecturer"] != "全部讲师")
                lecturer = context.Request["lecturer"];
            if (context.Request["startDate"] != null && context.Request["startDate"] != "" && context.Request["startDate"] != "开始日期")
                startDate = Convert.ToDateTime(context.Request["startDate"]);
            if (context.Request["endDate"] != null && context.Request["endDate"] != "" && context.Request["endDate"] != "结束日期")
                endDate = Convert.ToDateTime(context.Request["endDate"]);
            if (context.Request["isLine"] != null && context.Request["isLine"] != "")
                isLine = Convert.ToInt32(context.Request["isLine"]);
            DataRow[] list = bllEPCI.GetCourseInfo(keyWord, courseType, lecturer, isLine, startDate, endDate);
            StringBuilder html = new StringBuilder();
            StringBuilder pageStr = new StringBuilder();
            int counts = list.Count();
            //总页数
            int page = counts % iCount == 0 ? counts / iCount : counts / iCount + 1;
            int putinPage = iPage;
            if (iPage >= page)
                iPage = page;
            if (iPage < 1)
                iPage = 1;

            for (int i = (iPage - 1) * iCount; i < iPage * iCount; i++)
            {
                if (i >= counts)
                    break;
                DateTime courseStartDate = Convert.ToDateTime(list[i]["CourseStartDate"]);
                html.Append("<tr>");
                html.Append("<td class='blue text_left'><a href='courseinfo.aspx?id=" + list[i]["Id"] + "'>" + list[i]["Title"] + "</a></td>");
                html.Append("<td width='110'>" + list[i]["CourseType"] + "</td>");
                html.Append("<td width='110'>" + courseStartDate.ToShortDateString() + "<br />" + list[i]["StartTime"] + "-" + list[i]["EndTime"] + "</td>");
                if (list[i]["Scores"] == null || list[i]["Scores"].ToString() == "" || list[i]["Scores"].ToString() == "0.00")
                    html.Append("<td width='90' class='blue'>暂无</td>");
                else
                    html.Append("<td width='90' class='blue'>" + list[i]["Scores"] + "分</td>");

                if (courseStartDate < DateTime.Now)
                {
                    html.Append("<td width='90'><a href='javascript:void(0);'>已经结束</a></td>");
                    html.Append("<td width='90'><a href='javascript:void(0);'>已经结束</a></td>");
                }
                else
                {
                    if (Convert.ToInt32(list[i]["CountRes"]) < Convert.ToInt32(list[i]["IReservation"]))
                        html.Append("<td width='90'><a href='javascript:void(0);' title='请先预约' onclick=\"Reservation('" + list[i]["Id"] + "');\">我要预订</a></td>");
                    else
                        html.Append("<td width='90'><a href='javascript:void(0);' title='请先预约'>名额已满</a></td>");
                    html.Append("<td width='90'><a href='javascript:void(0);' onclick=\"GoListen('" + list[i]["Id"] + "')\" title='请先预约'>我要听课</a></td>");
                }
                if (list[i]["Attachment"] != null && list[i]["Attachment"].ToString() != "")
                    html.Append("<td width='90'><a href='" + list[i]["Attachment"] + "' target='_blank' onclick=\"AddCount('" + list[i]["Id"] + "');\">下载课件</a></td>");
                else
                    html.Append("<td width='90'><a href='javascript:void(0);' onclick=\"alert('暂无课件');\">下载课件</a></td>");
                html.Append("</tr>");
            }
            if (counts == 0)
                html.Append("<tr><td>暂无数据</td></tr>");

            //分页
            if (iPage == 1)
                pageStr.Append("<a href=\"javascript:void(0)\" class=\"btn txt\"><span>首页</span></a>");
            else
                pageStr.Append("<a href=\"javascript:void(0)\" onclick=\"GetCourseInfo('1')\" class=\"btn txt\"><span>首页</span></a>");
            //绑定上一页
            if (putinPage > 1)
                pageStr.Append("<a href=\"javascript:void(0)\" onclick=\"GetCourseInfo('p')\" class=\"btn txt\" style='width:40px;'><span>上一页</span></a>");
            else
                pageStr.Append("<a href=\"javascript:void(0)\" class=\"btn txt\" style='width:40px;'><span>上一页</span></a>");

            if (iPage > 10)
                pageStr.Append("<a href=\"javascript:void(0)\" onclick=\"GetCourseInfo('b')\" class=\"btn num some\"><span>...</span></a>");
            int j = 1;
            if (iPage > 10)
            {
                iPage = (iPage / 10) * 10 + 1;
                j = iPage;
                for (; j < iPage + 10; j++)
                {
                    if (j > page)
                        break;
                    if (j == iPage)
                        pageStr.Append("<a href=\"javascript:void(0)\" onclick=\"GetCourseInfo('" + j + "')\" class='btn num target'><span>" + j + "</span></a>");
                    else
                        pageStr.Append("<a href='javascript:void(0)' onclick=\"GetCourseInfo('" + j + "')\" class='btn num'><span>" + j + "</span></a>");
                }
            }
            else
            {
                for (; j < 11; j++)
                {
                    if (j > page)
                        break;
                    if (j == iPage)
                        pageStr.Append("<a href=\"javascript:void(0)\" onclick=\"GetCourseInfo('" + j + "')\" class='btn num target'><span>" + j + "</span></a>");
                    else
                        pageStr.Append("<a href='javascript:void(0)' onclick=\"GetCourseInfo('" + j + "')\" class='btn num'><span>" + j + "</span></a>");
                }
            }

            if (j < page)
                pageStr.Append("<a href='javascript:void(0)' onclick=\"GetCourseInfo('g')\" class=\"btn num some\"><span>...</span></a>");
            //绑定下一页
            if (putinPage < page)
                pageStr.Append("<a href='javascript:void(0)' onclick=\"GetCourseInfo('n')\" class=\"btn txt\" style='width:40px;'><span>下一页</span></a>");
            else
                pageStr.Append("<a href='javascript:void(0)' class=\"btn txt\" style='width:40px;'><span>下一页</span></a>");

            if (iPage == page)
                pageStr.Append("<a href='javascript:void(0)' class='btn txt'><span>末页</span></a>");
            else
                pageStr.Append("<a href='javascript:void(0)' onclick=\"GetCourseInfo('" + page + "')\" class='btn txt'><span>末页</span></a>");
            string[] ss = new string[] { html.ToString(), pageStr.ToString() };
            strReturn.AppendLine(common.ObjToJson(ss, Encoding.UTF8));
            return strReturn;
        }

        /// <summary>
        /// 获取课程信息
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private StringBuilder GetCourseInfo(HttpContext context)
        {
            StringBuilder strReturn = new StringBuilder();
            int iCount = 10;
            int iPage = Convert.ToInt32(context.Request["page"]);
            string keyWord = "", courseType = "", lecturer = "";
            DateTime startDate = DateTime.Now.AddYears(-10), endDate = DateTime.Now.AddYears(10);
            if (context.Request["keyWord"] != null && context.Request["keyWord"] != "关键字查找")
                keyWord = context.Request["keyWord"];
            if (context.Request["courseType"] != null && context.Request["courseType"] != "课程类别" && context.Request["courseType"] != "全部课程")
                courseType = context.Request["courseType"];
            if (context.Request["lecturer"] != null && context.Request["lecturer"] != "讲师搜索" && context.Request["lecturer"] != "讲师" && context.Request["lecturer"] != "全部讲师")
                lecturer = context.Request["lecturer"];
            if (context.Request["startDate"] != null && context.Request["startDate"] != "" && context.Request["startDate"] != "开始日期")
                startDate = Convert.ToDateTime(context.Request["startDate"]);
            if (context.Request["endDate"] != null && context.Request["endDate"] != "" && context.Request["endDate"] != "结束日期")
                endDate = Convert.ToDateTime(context.Request["endDate"]);
            DataRow[] list = bllEPCI.GetCourseInfo(keyWord, courseType, lecturer, 1, startDate, endDate);
            StringBuilder html = new StringBuilder();
            StringBuilder pageStr = new StringBuilder();
            int counts = list.Count();
            //总页数
            int page = counts % iCount == 0 ? counts / iCount : counts / iCount + 1;
            int putinPage = iPage;
            if (iPage >= page)
                iPage = page;
            if (iPage < 1)
                iPage = 1;

            for (int i = (iPage - 1) * iCount; i < iPage * iCount; i++)
            {
                if (i >= counts)
                    break;
                DateTime courseStartDate = Convert.ToDateTime(list[i]["CourseStartDate"]);
                html.Append("<tr>");
                html.Append("<td class='blue' style='text-align:left; padding-left:20px;'><a href='courseinfo.aspx?id=" + list[i]["Id"] + "'>" + list[i]["Title"] + "</a></td>");
                html.Append("<td width='110'>" + list[i]["CourseType"] + "</td>");
                html.Append("<td width='94'>" + list[i]["Lecturer"] + "</td>");
                html.Append("<td width='110'>" + courseStartDate.ToShortDateString() + "<br />" + list[i]["StartTime"] + "-" + list[i]["EndTime"] + "</td>");
                if (list[i]["Scores"] == null || list[i]["Scores"].ToString() == "" || list[i]["Scores"].ToString() == "0.00")
                    html.Append("<td width='90' class='blue'>暂无</td>");
                else
                    html.Append("<td width='90' class='blue'>" + list[i]["Scores"] + "分</td>");

                //if (list[i]["Score"] == null || list[i]["Score"].ToString() == "" || list[i]["Score"].ToString() == "0")
                //{
                //html.Append("<td width='90' class='blue'>暂无</td>");
                //}
                //else
                //{
                //    html.Append("<td width='90' class='blue'>" + list[i]["Score"] + "分</td>");
                //}

                if (courseStartDate < DateTime.Now)
                {
                    html.Append("<td width='90'><a href='javascript:void(0);'>已经结束</a></td>");
                    html.Append("<td width='90'><a href='javascript:void(0);'>已经结束</a></td>");
                }
                else
                {
                    if (Convert.ToInt32(list[i]["CountRes"]) < Convert.ToInt32(list[i]["IReservation"]))
                        html.Append("<td width='90'><a href='javascript:void(0);' title='请先预约' onclick=\"Reservation('" + list[i]["Id"] + "');\">我要预订</a></td>");
                    else
                        html.Append("<td width='90'><a href='javascript:void(0);' title='请先预约'>名额已满</a></td>");
                    html.Append("<td width='90'><a href='javascript:void(0);' onclick=\"GoListen('" + list[i]["Id"] + "')\" title='请先预约'>我要听课</a></td>");
                }
                if (list[i]["Attachment"] != null && list[i]["Attachment"].ToString() != "")
                    html.Append("<td width='90'><a href='" + list[i]["Attachment"] + "' target='_blank' onclick=\"AddCount('" + list[i]["Id"] + "');\">下载课件</a></td>");
                else
                    html.Append("<td width='90'><a href='javascript:void(0);' onclick=\"alert('暂无课件');\">下载课件</a></td>");
                html.Append("</tr>");
            }
            if (counts == 0)
                html.Append("<tr><td>暂无数据</td></tr>");

            //分页
            if (iPage == 1)
                pageStr.Append("<a href=\"javascript:void(0)\" class=\"btn txt\"><span>首页</span></a>");
            else
                pageStr.Append("<a href=\"javascript:void(0)\" onclick=\"GetCourseInfo('1')\" class=\"btn txt\"><span>首页</span></a>");
            //绑定上一页
            if (putinPage > 1)
                pageStr.Append("<a href=\"javascript:void(0)\" onclick=\"GetCourseInfo('p')\" class=\"btn txt\" style='width:40px;'><span>上一页</span></a>");
            else
                pageStr.Append("<a href=\"javascript:void(0)\" class=\"btn txt\" style='width:40px;'><span>上一页</span></a>");

            if (iPage > 10)
                pageStr.Append("<a href=\"javascript:void(0)\" onclick=\"GetCourseInfo('b')\" class=\"btn num some\"><span>...</span></a>");
            int j = 1;
            if (iPage > 10)
            {
                iPage = (iPage / 10) * 10 + 1;
                j = iPage;
                for (; j < iPage + 10; j++)
                {
                    if (j > page)
                        break;
                    if (j == iPage)
                        pageStr.Append("<a href=\"javascript:void(0)\" onclick=\"GetCourseInfo('" + j + "')\" class='btn num target'><span>" + j + "</span></a>");
                    else
                        pageStr.Append("<a href='javascript:void(0)' onclick=\"GetCourseInfo('" + j + "')\" class='btn num'><span>" + j + "</span></a>");
                }
            }
            else
            {
                for (; j < 11; j++)
                {
                    if (j > page)
                        break;
                    if (j == iPage)
                        pageStr.Append("<a href=\"javascript:void(0)\" onclick=\"GetCourseInfo('" + j + "')\" class='btn num target'><span>" + j + "</span></a>");
                    else
                        pageStr.Append("<a href='javascript:void(0)' onclick=\"GetCourseInfo('" + j + "')\" class='btn num'><span>" + j + "</span></a>");
                }
            }

            if (j < page)
                pageStr.Append("<a href='javascript:void(0)' onclick=\"GetCourseInfo('g')\" class=\"btn num some\"><span>...</span></a>");
            //绑定下一页
            if (putinPage < page)
                pageStr.Append("<a href='javascript:void(0)' onclick=\"GetCourseInfo('n')\" class=\"btn txt\" style='width:40px;'><span>下一页</span></a>");
            else
                pageStr.Append("<a href='javascript:void(0)' class=\"btn txt\" style='width:40px;'><span>下一页</span></a>");

            if (iPage == page)
                pageStr.Append("<a href='javascript:void(0)' class='btn txt'><span>末页</span></a>");
            else
                pageStr.Append("<a href='javascript:void(0)' onclick=\"GetCourseInfo('" + page + "')\" class='btn txt'><span>末页</span></a>");
            string[] ss = new string[] { html.ToString(), pageStr.ToString() };
            strReturn.AppendLine(common.ObjToJson(ss, Encoding.UTF8));
            return strReturn;
        }
    }
}